package main

import (
	"fmt"
	"runtime"
	"time"
)

var num int

func counter(numChan chan<- int, num int) {
	for i := 1; i < num; i++ {
		numChan <- i
	}
	close(numChan)
}
func Fx(resChan chan map[int]int, numChan <-chan int, ex chan bool) {

	for c := range numChan {
		res := make(map[int]int)
		var sum int
		for i := 1; i <= c; i++ {
			sum = sum + i
		}
		res[c] = sum
		resChan <- res
	}
	ex <- true

}
func CloseAaction(flag chan bool, res chan map[int]int, start time.Time) {
	for i := 0; i < 4; i++ {
		<-flag
	}
	close(res)
	fmt.Println(time.Since(start))
	close(flag)
}
func main() {

	runtime.GOMAXPROCS(4)
	num = 200000

	numChan := make(chan int, num)
	resChan := make(chan map[int]int, num)
	flag := make(chan bool, 4)
	start := time.Now()

	go counter(numChan, num)

	for i := 0; i < 4; i++ {
		go Fx(resChan, numChan, flag)
	}

	for i := 0; i < 4; i++ {
		<-flag
	}

	close(resChan)
	fmt.Println(time.Since(start))
	close(flag)

	// 	for {
	// 		s, ok := <-resChan
	// 		if !ok {
	// 			break
	// 		}
	// 		fmt.Println(s)
	// 	}

}
